import {ProcedureBase} from "../../AzraelFrameWork/src/Procedure/ProcedureBase";
import {GameMode} from "../Game/GameMode";
import {BaseGame} from "../Game/BaseGame";
import {IFsm} from "../../AzraelFrameWork/src/Fsm/IFsm";
import {IProcedureManager} from "../../AzraelFrameWork/src/Procedure/IProcedureManager";
import {NormalGame} from "../Game/NormalGame";
import {Constant} from "../Definition/Constant";
import {UIStruct} from "../UI/UIStruct";
import GameMainEntry from "../Base/GameMainEntry";
import ccclass = cc._decorator.ccclass;
//https://github.com/timohausmann/quadtree-js //四叉树
type ProcedureOwner = IFsm<IProcedureManager>;
@ccclass("ProcedureMain")
export  class ProcedureMain extends ProcedureBase{
    private readonly _games:Map<GameMode,BaseGame> = new Map<GameMode, BaseGame>();


    private _currentgame:BaseGame;

    OnInit(fsm: ProcedureOwner): void {
        super.OnInit(fsm);
        let normalGame:NormalGame = GameMainEntry.UI.getComponentInChildren(NormalGame);
        this._games.set(GameMode.Normal, normalGame);
    }

    OnEnter(fsm: ProcedureOwner): void {
        super.OnEnter(fsm);
        let gamemode:GameMode = cc.sys.localStorage.getItem(Constant.gamemode);
        if(gamemode == null){
            gamemode = GameMode.Normal;
        }
        this._currentgame = this._games.get(gamemode);
        this._currentgame.Init();
    }

    OnUpdate(fsm: ProcedureOwner, elapseSeconds: number, realElapseSeconds: number): void {
        super.OnUpdate(fsm, elapseSeconds, realElapseSeconds);
        if(this._currentgame != null){
            if(this._currentgame.GameOver()){
            }else{
                this._currentgame?.Update(elapseSeconds, realElapseSeconds);
            }
        }


    }

    OnLeave(fsm: ProcedureOwner, isShutdown: boolean): void {
        super.OnLeave(fsm, isShutdown);
        this._currentgame?.Shutdown();
    }

    OnDestroy(fsm: ProcedureOwner): void {
        super.OnDestroy(fsm);
        this._games.clear();
    }
}